#!/usr/bin/env python
import numpy as np
from astropy.io import fits
from glob import glob
import sys
import os

for root,dirs,files in os.walk(sys.argv[1]):
    for name in dirs:
        d = os.path.join(root,name)
        files = glob(d+"/*.fits")
        for i in range(len(files)):
            print("cropping file "+files[i].split('/')[-1])
            data = fits.getdata(files[i])
            header = fits.open(files[i])[0].header
            data_crop = data.copy()
            if sys.argv[2] == '0083':
                data_crop[4700:] = data_crop[:4700]
            if sys.argv[2] == '0095':
                data_crop[:4700] = data_crop[4700:]
            fits.writeto(files[i],data=data_crop,header=header,overwrite=True)
